package com.java;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.tp.Constant;
import com.tp.CustomHttpClient;
import com.tp.GetRes;
import com.tp.R;

public class ItemAdapter extends ArrayAdapter<Item> implements LocationListener {

	private Context context;
	LocationManager lm = null;
	GeoPoint p = null;
	static double latitude;
	static double longitude;
	int flag = 0;
	Location location;
	ArrayList<Integer> locations;
	ArrayList<Integer> objs = new ArrayList<Integer>();

	public ItemAdapter(Context context, int layoutResourceId,
			ArrayList<Item> items) {
		super(context, layoutResourceId, items);
		// TODO Auto-generated constructor stub
		this.context = context;
		lm = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		// LocationListener mlocListener = new MyLocationListener();
		location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		if (location != null) {
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			if (latitude > 0 && longitude > 0) {
				locations = getMinDistance(latitude, longitude);
				for (int i = 0; i < locations.size(); i++) {
					Log.v("i", "" + locations.get(i));
					int id_Obj = getObjectByLocation(locations.get(i));
					objs.add(id_Obj);
				}

			}
		}
	}

	private class ItemHolder {
		ImageView icon;
		TextView name;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Item item = getItem(position);

		ItemHolder holder = null;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			Log.v("ConvertView", "NULL");
			convertView = inflater.inflate(R.layout.list_item, null);

			holder = new ItemHolder();

			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.name = (TextView) convertView.findViewById(R.id.name);

			convertView.setTag(holder);
		} else {
			holder = (ItemHolder) convertView.getTag();
			Log.v("ConvertView", "!NULL");

		}
		// RelativeLayout LinLayout = (RelativeLayout)
		// convertView.findViewById(R.id.layout_item);
		holder.icon.setImageResource(item.getIcon());
		holder.name.setText(item.getName());

		for (int i = 0; i < objs.size(); i++) {
			int id_Obj = objs.get(i);
			if (item.getId() == id_Obj ) {
				Log.v("Item id", " " + item.getId() + "id Obj" + id_Obj);
				convertView.setBackgroundColor(context.getResources().getColor(
						R.color.light));
				//flag = 1;
				break;
			} else 
				convertView.setBackgroundColor(Color.WHITE);
		}
		// convertView.setBackgroundColor(Color.BLUE);
		return convertView;

	}

	private ArrayList<Integer> getMinDistance(double lat, double longi) {
		JSONArray jArray;
		JSONObject json_data = null;
		ArrayList<Integer> a = new ArrayList<Integer>();
		int i;
		int id_Location = 0;
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("lat", Double.toString(lat)));
		postParameters.add(new BasicNameValuePair("longi", Double
				.toString(longi)));

		String result = null;
		try {
			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/LocationController/getMinDistance", postParameters);

		} catch (Exception e) {
		}

		try {
			jArray = new JSONArray(result);
			// jArr = jArray;
			for (i = 0; i < jArray.length(); i++) {
				json_data = jArray.getJSONObject(i);
				id_Location = json_data.getInt("id_Location");
				a.add(id_Location);
			}
		} catch (Exception e) {
		}
		return a;
	}

	private int getObjectByLocation(int id_Location) {
		JSONArray jArray;
		JSONObject json_data = null;

		int i;
		int id_Obj = 0;
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("id_Location", Integer
				.toString(id_Location)));

		String result = null;
		try {
			result = CustomHttpClient
					.executeHttpPost(Constant.host
							+ "/ObjectController/getObjectByIdLocation",
							postParameters);

		} catch (Exception e) {
		}

		try {
			jArray = new JSONArray(result);
			// jArr = jArray;
			for (i = 0; i < jArray.length(); i++) {
				json_data = jArray.getJSONObject(i);
				id_Obj = json_data.getInt("id_Obj");

			}
		} catch (Exception e) {
		}
		return id_Obj;
	}

	@Override
	public void onLocationChanged(Location location) {

		lm.removeUpdates(this);
		this.location = location;
		latitude = (float) location.getLatitude();
		longitude = (float) location.getLongitude();
		Log.v("lat", "Update" + latitude);
		Log.v("long", "Update" + longitude);
	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

}
